package com.ljq.software_springboot.softwareprogram.mapper;

import com.ljq.software_springboot.softwareprogram.entity.ChatMessage;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author author
 * @since 2024-12-29
 */
@Mapper
public interface ChatMessageMapper extends BaseMapper<ChatMessage> {
    @Select("SELECT " +
            "g.id AS id, " +
            "g.sender_id AS sender_id, " +
            "g.recipient_id AS recipient_id, " +
            "g.content AS content, " +
            "g.timestamp AS timestamp, " +
            "sa.username AS sender, " +
            "ra.username AS recipient " +
            "FROM chat_message g " +
            "JOIN account sa ON g.sender_id = sa.id " +
            "JOIN account ra ON g.recipient_id = ra.id " +
            "WHERE (g.sender_id = #{senderId} AND g.recipient_id = #{recipientId}) " +
            "OR (g.sender_id = #{recipientId} AND g.recipient_id = #{senderId}) " +
            "ORDER BY g.timestamp")
    List<ChatMessage> getMessages(@Param("senderId") String senderId, @Param("recipientId") String recipientId);
}
